package com.mall.server.service.ums;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.api.*;
import com.mall.model.domain.*;
import com.mall.model.vo.CommonPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UmsRoleService {

    @DubboReference
    private UmsRoleApi umsRoleApi;

    @DubboReference
    private UmsMenuApi umsMenuApi;

    @DubboReference
    private UmsRoleMenuRelationApi umsRoleMenuRelationApi;

    @DubboReference
    private UmsRoleResourceRelationApi umsRoleResourceRelationApi;

    @DubboReference
    private UmsResourceApi umsResourceApi;

    //根据角色名称分页获取角色列表
    public CommonPage page(String keyword, Integer pageNum, Integer pageSize) {

        Page<UmsRole> page = umsRoleApi.findByName(keyword, pageNum, pageSize);

        return new CommonPage(page);
    }

    //添加角色
    public void createRole(UmsRole role) {

        role.setCreateTime(new Date());

        umsRoleApi.createRole(role);

    }

    //给角色分配菜单
    public void allocMenu(Integer[] menuIds, Integer roleId) {

        umsRoleMenuRelationApi.deleteByRoleId(roleId);
        ArrayList<UmsRoleMenuRelation> list = new ArrayList<>();

        for (Integer menuId : menuIds) {
            if (menuId != null) {
                UmsRoleMenuRelation urm = new UmsRoleMenuRelation();
                urm.setRoleId(Long.valueOf(roleId));
                urm.setMenuId(Long.valueOf(menuId));
                list.add(urm);
            }
        }

        umsRoleMenuRelationApi.saveBatch(list);
    }

    //给角色分配资源
    public void allocResource(Integer[] resourceIds, Integer roleId) {

        umsRoleApi.deleteByRoleId(roleId);

        ArrayList<UmsRoleResourceRelation> list = new ArrayList<>();

        for (Integer resourceId : resourceIds) {
            if (resourceId != null) {
                UmsRoleResourceRelation urm = new UmsRoleResourceRelation();
                urm.setRoleId(Long.valueOf(roleId));
                urm.setResourceId(Long.valueOf(resourceId));
                list.add(urm);
            }
        }
        umsRoleResourceRelationApi.saveBatch(list);

    }

    //批量删除角色
    public void delete(List<Integer> ids) {

        List<Long> list = ids.stream().map(e -> Long.valueOf(e)).collect(Collectors.toList());

        umsRoleApi.delete(list);

        List<UmsRoleMenuRelation> urms = umsRoleMenuRelationApi.findByIds(list);
        List<UmsRoleResourceRelation> urrs = umsRoleResourceRelationApi.findByIds(list);

        if (!CollUtil.isEmpty(urms)) {
            umsRoleMenuRelationApi.delete(list);

        }
        if (!CollUtil.isEmpty(urrs)) {
            umsRoleResourceRelationApi.delete(list);
        }
    }

    //获取所有角色
    public List<UmsRole> listAll() {

        List<UmsRole> list = umsRoleApi.listAll();

        if (CollUtil.isEmpty(list)) {
            return null;
        }

        return list;

    }

    //获取角色相关菜单
    public List<UmsMenu> listMenu(Integer roleId) {

        List<UmsRoleMenuRelation> list = umsRoleMenuRelationApi.findByRoleId(roleId);

        if (CollUtil.isEmpty(list)) {
            return null;
        }

        List<Long> menuIds = CollUtil.getFieldValues(list, "menuId", Long.class);

        List<UmsMenu> umsMenus = umsRoleApi.listMenu(menuIds);

        return umsMenus;
    }

    //获取角色相关资源
    public List<UmsResource> listResource(Integer roleId) {

        List<UmsRoleResourceRelation> list = umsRoleResourceRelationApi.findByRoleId(roleId);
        if (CollUtil.isEmpty(list)) {
            return null;
        }
        List<Long> resourceIds = CollUtil.getFieldValues(list, "resourceId", Long.class);

        return umsResourceApi.listResource(resourceIds);

    }

    //修改角色
    public void updateRole(Integer id, UmsRole role) {

        umsRoleApi.updateRole(id, role);

    }

    public void updateStatus(Integer id, Integer status) {

        UmsRole umsRole = new UmsRole();
        umsRole.setStatus(status);

        umsRoleApi.updateRole(id, umsRole);

    }


}
